﻿function Write()
{
	if ($.trim(this.Name.value) == "")
	{
		Alert(_lang_name_empty, function(){this.Name.select();}, this);
		return false;
	}
	
	if ($.trim(this.Subject.value) == "")
	{
		Alert(_lang_subject_empty, function(){this.Subject.select();}, this);
		return false;
	}
	
	var content;
	if ($("#Content").length == 1)
	{
	    var editor = FCKeditorAPI.GetInstance("Content");
	    if (!editor.IsDirty())
		{
		    Alert(_lang_content_empty, function(){this.Content.select();}, this);
		    return false;
		}
		
		content = editor.GetXHTML(false);
	}
	else
	{
	    if ($.trim(this.Content.value) == "")
	    {
		    Alert(_lang_content_empty, function(){this.Content.select();}, this);
		    return false;
	    }
	    
	    content = this.Content.value;
	}
	
	if (this.QQ.value != "" && !this.QQ.value.match(/^\d+$/))
	{
		Alert(_lang_qq_not_correct, function(){this.QQ.select();}, this);
		return false;
	}
	
	if (this.Email.value != "" && !this.Email.value.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/))
	{
		Alert(_lang_email_not_correct, function(){this.Email.select();}, this);
		return false;
	}
	
	var data = "IsChecked=" + !_isNeedToCheck;
	data += "&Name=" + encodeURIComponent(this.Name.value);
	data += "&Sex=" + encodeURIComponent(this.Sex.value);
	data += "&Subject=" + encodeURIComponent(this.Subject.value);
    data += "&Content=" + encodeURIComponent(content);
	data += "&Private=" + encodeURIComponent(this.Private.checked);
	data += "&Password=" + encodeURIComponent(this.Password.value);
	data += "&ParentID=" + encodeURIComponent(this.ParentID.value);
	data += "&Head=" + encodeURIComponent($("#HeadPicture").attr("src").substr("http://" + document.domain));
	if (this.QQ.value != "")
		data += "&QQ=" + this.QQ.value;
	if (this.Email.value != "")
		data += "&Email=" + encodeURIComponent(this.Email.value);
	if (this.CategoryID != null)
	{
		data += "&CategoryID=" + this.CategoryID.value;
		data += "&CategoryName=" + $(this.CategoryID).data("ame");
	}
	if (this.TypeID != null)
	{
		data += "&TypeID=" + this.TypeID.value;
		data += "&TypeName=" + $(this.TypeID).data("name");
	}
	
	$(":submit")[0].disabled = true;
	SetStatusMessage(_lang_working);

	$.ajax(
	{
		url : _webPath + "alicms.aspx?Object=GuestBook&Operation=Add",
		data : data,
		type : "POST",
		error : function (request)
		{
			Alert(_lang_operation_fail);
	        SetStatusMessage();
			$(":submit")[0].disabled = false;
		},
		success : function  (data)
		{
			Alert(_lang_operation_success);
	        SetStatusMessage();
			$(":submit")[0].disabled = false;
			
			$("form:last")[0].reset();
		}
	});

	return false;
}

function Init()
{
	if (typeof(SetupIme) != "undefined")
		SetupIme();

	//键盘处理“上一页”，“下一页”
	$("body").keyup(function(e)
	{
		if (e.target.tagName == "INPUT" || e.target.tagName == "TEXTAREA")
			return;

		if (e.keyCode == 37)//上一页
		{
			if ($("#PrevPage").length > 0)
				window.location = $("#PrevPage").attr("href");
		}
		else if (e.keyCode == 39)//下一页
		{
			if ($("#NextPage").length > 0)
				window.location = $("#NextPage").attr("href");
		}
	});
	
	//ToolTip
	if (typeof(_lang) != "undefined" && (_lang == "UYG" || _lang == "KZA" || _lang == "KGA"))
		SetTip();
	
	if (_page == "index")
	{
	    $("[name=Head]").change(function()
	    {
		    var pic = $("#HeadPicture");
		    var slash = pic.attr("src").lastIndexOf("/");
		    var src = pic.attr("src").substr(0, slash + 1);
		    pic.attr("src", src + this.options[this.selectedIndex].text + ".gif");
	    });
    		
	    $("form:last").submit(Write);
	    $("#Private").click(function(){this.form.Password.disabled = !this.checked;});
	    
	    $("h1 a").click(function()
	    {
	        $("[name=Subject]").val($(this).attr("title") == "" ? $(this).attr("tip") : $(this).attr("title"));
	        $("[name=ParentID]").val(this.id.substr(1));
	        $("[name=Subject]").focus();
		    window.scrollBy(0, 300);
	        return false;
	    });
	}
}
Init();